/*
 * RemoveCompletedAction.java
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.actions.list;

import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import org.tiberiumlabs.lailaps.actions.AbstractResourcedAction;
import org.tiberiumlabs.lailaps.actions.events.DataEvent;
import org.tiberiumlabs.lailaps.list.DownloadItem;
import org.tiberiumlabs.lailaps.list.DownloadsListModel;
import org.tiberiumlabs.lailaps.list.StatusConstants;
import org.tiberiumlabs.lailaps.resources.ImageLoader;
import org.tiberiumlabs.lailaps.settings.Settings;
import org.tiberiumlabs.lailaps.threads.ThreadsMap;
import org.tiberiumlabs.lailaps.toolkit.StateManager;

/**
 *
 * @author <a href="mailto:ss.paranoid@gmail.com">Paranoid</a>
 */
public class RemoveCompletedAction extends AbstractResourcedAction implements StatusConstants {
    
    protected final ThreadsMap threadsMap = mediator.getThreadsMap();
    
    /** Creates a new instance of RemoveCompletedAction */
    public RemoveCompletedAction() {
        // TODO set other resource bundles
        putValue(NAME, bundle.getString("list.removecompleted.name"));
        putValue(SHORT_DESCRIPTION, bundle.getString("list.removecompleted.short"));
        putValue(LONG_DESCRIPTION, bundle.getString("list.removecompleted.long"));
        putValue(LARGE_ICON_KEY, ImageLoader.getRemoveIcon());
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        
        // check if we even need to ask:
        if (Settings.getApplicationSettings().isAskBeforeRemove()) {
            // if we need to ask and user dont choose OK - return out of here.
            // we dont need to check for visibility of main window here, because we cant even invoke this action without main window visible.
            if (JOptionPane.showConfirmDialog(mediator.getGuiBuilder().getMainWindow(), bundle.getString("list.remove.confirmation.message"), bundle.getString("list.remove.confirmation.title"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
                return;
            }
        }
        
        DownloadsListModel model = mediator.getDownloadsListModel();
        int rowCount = model.getRowCount();
        
        int count = 0;
        for (int i = rowCount-1; i >= 0; i--) {
            DownloadItem item = model.getItem(i);
            if (item.getStatus() == COMPLETED) {
                count++;
                model.removeItem(i);
            }
        }
        
        if (count > 0) {
            threadsMap.notify(new DataEvent(DataEvent.REMOVED, 0, rowCount, count));
            // count statistics
            mediator.getStateManager().remove(StateManager.COMPLETED, count);
        }
        
    }
    
}
